<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'maintenance:process:maintenanceTaskProcess:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.maintenanceTaskId }}
        </a>
      </template>
      <template #abnormalPic="{ record }">
        <Image :src="record.abnormalPic" width="100" height="100" alt="" />
      </template>
      <template #resultPicUrl="{ record }">
        <Image :src="record.resultPicUrl" width="100" height="100" alt="" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsMaintenanceProcessMaintenanceTaskProcessList">
  import { unref,ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { maintenanceTaskProcessDelete, maintenanceTaskProcessListData } from '/@/api/maintenance.process/maintenanceTaskProcess';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
import { unitRegionListData } from '/@/api/unitmanage/unitRegion';

  const { t } = useI18n('maintenance.process.maintenanceTaskProcess');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('维保任务处理流程记录表管理'),
  };

  const EnterpriseParams = ref<Recordable>({
     name:''
    });

  const workTaskParams = ref<Recordable>({
     name:''
    });
  const regionParams = ref<Recordable>({
     name:''
  })

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'enterpriseName',
        component: 'Select',
        componentProps: () => {
        return {
          placeholder: t('请输入所属主体'),
          api: unitEnterpriseDominantListData,
          params: EnterpriseParams.value,
          fieldNames: { label: 'name', value: 'unitCode' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            EnterpriseParams.value.name = value;
            console.log(EnterpriseParams.value);
          },
        };
      },
      },
      {
        label: t(''),
        field:'id',
        fieldLabel: 'name',
        component: 'Select',
        componentProps: () => {
        return {
          placeholder: t('请输入工单名称'),
          api: maintenanceTaskProcessListData,
          params: workTaskParams.value,
          fieldNames: { label: 'name', value: 'id' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            workTaskParams.value.name = value;
            console.log(workTaskParams.value);
          },
        };
      },
      },
      {
        label: t(''),
        field: 'reportTime',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          placeholder: ['请选择上报时间', '请选择上报时间'],
        },
      },
      {
        label: t(''),
        field:'id',
        fieldLabel: 'name',
        component: 'Select',
        componentProps: () => {
        return {
          placeholder: t('请输入区域名称'),
          api: unitRegionListData,
          params: regionParams.value,
          fieldNames: { label: 'name', value: 'id' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            regionParams.value.name = value;
            console.log(regionParams.value);
          },
        };
      },
      },
      {
        label: t(''),
        field: 'supervisorName',
        component: 'Input',
        componentProps:{
          placeholder: t('请输入监督员姓名'),
      }
    },
      
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('所属企业名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('分配人姓名'),
      dataIndex: 'assignerName',
      key: 'a.assigner_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工单名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('地点'),
      dataIndex: 'address',
      key: 'a.address',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('异常图片'),
      dataIndex: 'abnormalPic',
      key: 'a.abnormal_pic',
      sorter: true,
      width: 130,
      align: 'left',
      slots: { customRender: 'abnormalPic' },
    },
    {
      title: t('隐患描述'),
      dataIndex: 'remark',
      key: 'a.remark',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('处理结果图片'),
      dataIndex: 'resultPicUrl',
      key: 'a.result_pic_url',
      sorter: true,
      width: 130,
      align: 'left',
      slots: { customRender:'resultPicUrl' },
    },
    {
      title: t('上报时间'),
      dataIndex: 'reportTime',
      key: 'a.report_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('区域名称'),
      dataIndex: 'regionName',
      key: 'a.region_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('处理方式'),
      dataIndex: 'handlingMethod',
      key: 'a.handling_method',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('超期情况'),
      dataIndex: 'isOverdue',
      key: 'a.is_overdue',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('监督员姓名'),
      dataIndex: 'supervisorName',
      key: 'a.supervisor_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑维保任务处理流程记录表'),
        onClick: handleForm.bind(this, { tid: record.tid }),
        auth: 'maintenance:process:maintenanceTaskProcess:edit',
      },
      {
        icon: 'i-fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
      // {
      //   icon: 'i-ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除维保任务处理流程记录表'),
      //   popConfirm: {
      //     title: t('是否确认删除维保任务处理流程记录表'),
      //     confirm: handleDelete.bind(this, record),
      //   },
      //   auth: 'maintenance:process:maintenanceTaskProcess:edit',
      //   ifShow: () => record.status == '9',
      // },
    ],
  };

  const [registerTable, { reload }] = useTable({
    api: maintenanceTaskProcessListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await maintenanceTaskProcessDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'maintenance_process', bizKey: record.id });
  }
</script>
